package com.ojoin.trade.supplyloan_core.web.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jms.Destination;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ojoin.trade.bpm.web.service.BpmTaskService;
import com.ojoin.trade.common.Constants;
import com.ojoin.trade.common.constdef.IConstDef;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.ControllerException;
import com.ojoin.trade.common.interceptor.AvoidDuplicateSubmission;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.DownloadUtils;
import com.ojoin.trade.common.utils.FileUploadUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.web.controller.SimpleBaseController;
import com.ojoin.trade.common.web.domain.Pair;
import com.ojoin.trade.common.web.domain.Result;
import com.ojoin.trade.common.web.domain.Result.Status;
import com.ojoin.trade.common.web.domain.SessionUser;
import com.ojoin.trade.common.web.domain.TbUploadFile;
import com.ojoin.trade.common.web.service.BaseService;
import com.ojoin.trade.common.web.service.TbUploadFileService;
import com.ojoin.trade.common_core.web.domain.TbMerchantInfo;
import com.ojoin.trade.common_core.web.service.BranchBaseService;
import com.ojoin.trade.common_core.web.service.TbMerchantInfoBaseService;
import com.ojoin.trade.supplyloan_core.constdef.ISLConstDef;
import com.ojoin.trade.supplyloan_core.web.domain.SLApplyBusiVO;
import com.ojoin.trade.supplyloan_core.web.domain.SlLoanNotPaybackVo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlAlipayInfo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanApplication;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUserVO;
import com.ojoin.trade.supplyloan_core.web.service.TbSlAlipayInfoService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlLoanApplicationService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlLoanInfoService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlPlatformUserService;

@Controller
@RequestMapping("/slApply")
public class SlApplyController extends SimpleBaseController{

    private org.slf4j.Logger log = LoggerFactory
            .getLogger(SlApplyController.class);

    
    @Autowired
    private TbUploadFileService tbUploadFileService;
    
    @Autowired
    private TbMerchantInfoBaseService tbMerchantInfoBaseService;
    
    @Autowired
    private BranchBaseService branchBaseService;

    @Autowired
    private TbSlPlatformUserService service;

    @Autowired
    private TbSlLoanApplicationService slLoanApplicationService;

    @Autowired
    private TbSlLoanInfoService slLoanInfoService;
    
    @Autowired
    private TbSlAlipayInfoService slAlipayInfoService; 
    
	
    @Autowired  
    @Qualifier("creditLogQueueDestination")  
    private Destination destination;  
    
    protected BaseService<TbSlPlatformUser> getBaseService() {
        return service;
    }

    @ResponseBody
    @RequestMapping(value = "/querySLApply", method = RequestMethod.GET)
    public Result querySLApply(HttpServletRequest request) {
        SessionUser uservo = CommonUtils.getSessionUser(request);

        if ("00".equals(uservo.getMchType())) {
            throw new BusinessException("供应链贷暂时未对个人用户开放!");
        }

        TbSlLoanApplication slLoan = slLoanApplicationService.queryTbSlApplicationByJoinId(uservo.getId());
        if (null == slLoan) {
            LogUtils.info(log, String.format("访问申请供应链贷"));
        }
        return new Result(Result.Status.OK, slLoan);
    }


    @AvoidDuplicateSubmission(needRemoveToken = true)
    @ResponseBody
    @RequestMapping(value = "/RealName", method = RequestMethod.POST)
    public Result realName(HttpServletRequest request, TbMerchantInfo info) {
        SessionUser uservo = CommonUtils.getSessionUser(request);
        Integer joinId = uservo.getId();
        info.setIdOfTbMerchantUser(joinId);
        info.setMhtNo(uservo.getMhtNo());
        int merchantInfoId=-1;
        try {
        	//实名已经通过不在更新
        	if(!info.isRealNameStatusOK()){
        		//三证处理
        		if("3".equals(info.getDoctype())){
        			info.setTaxNo(info.getLicNo());
        			info.setOrgNo(info.getLicNo());
        		}
        		info.setRealStatus("1");
        		tbMerchantInfoBaseService.addTbMerchantInfo(info);
        	}
            slLoanApplicationService.updateLoanStatusById(joinId, ISLConstDef.SLLOANSTATUS_REALNAME);
            merchantInfoId=info.getId();
        } catch (Exception e) {
            LogUtils.error(log, e);
            throw new BusinessException("保存资质资料失败", e);
        }

        return new Result(Result.Status.OK, merchantInfoId);
    }

    @ResponseBody
    @RequestMapping(value = "/saveBuis", method = RequestMethod.POST)
    public Result saveBuis(HttpServletRequest request,@RequestBody SLApplyBusiVO slApplyBusi) {
        SessionUser uservo = CommonUtils.getSessionUser(request);
        Integer joinId = uservo.getId();
        int joinuserid = CommonUtils.getSessionUser(request).getId();
        TbSlLoanApplication slLoan = slLoanApplicationService.queryTbSlApplicationByJoinId(joinuserid);
        if (null == slLoan) {
            return new Result(Status.ERROR, "您没有申请供应链贷.");
        } else {
            slLoanApplicationService.updateCreditInformationAndContract(joinuserid, slApplyBusi.getEvaluateFile(),slApplyBusi.getUpDownStreamContractFile());
        }
        
        List<TbSlPlatformUser> platformUser=slApplyBusi.getPlatformUser();
        if (CollectionUtils.isNotEmpty(platformUser)) {
            for (TbSlPlatformUser puser : platformUser) {
            	puser.buildDefaultEntity();
                puser.setIdOfTbMerchantUser(joinId);
                puser.encPassword();
            }
            service.insertAndDeleteSlPlatformUser(platformUser);
        }
        
        return new Result(Result.Status.OK, "业务资料保存成功");
    }
    
    @ResponseBody
    @RequestMapping(value = "/querySLBusiInfo", method = RequestMethod.GET)
    public Result querySLBusiInfo(HttpServletRequest request) {
        SessionUser uservo = CommonUtils.getSessionUser(request);
        int joinid = uservo.getId();
        TbMerchantInfo realName = tbMerchantInfoBaseService.queryInfoByJoinId(joinid);
        if(null!=realName){
        	String displayBranchNo=branchBaseService.queryDisplayBranchNo(realName.getBranchNo());
        	realName.setDisplayBranchNo(displayBranchNo);
        }
        TbSlLoanApplication slLoan = slLoanApplicationService.queryTbSlApplicationByJoinId(joinid);
        List<TbSlPlatformUserVO> platformUsers = service.selectAllPlatformUserListByJoinID(joinid,null);
        for(TbSlPlatformUserVO platformUser:platformUsers){
        	platformUser.decPassword();
        }
        
        String originalName="";
        String upDownStreamContractFileId=slLoan.getContract();
        if(StringUtils.isNotEmpty(upDownStreamContractFileId)){
        	TbUploadFile upDownStreamContractFile=tbUploadFileService.queryById(Integer.parseInt(upDownStreamContractFileId));
        	if(null!=upDownStreamContractFile){
        		originalName = upDownStreamContractFile.getOriginalName();        	
        	}else{
        		originalName="文件保存失效，请重新上传";
        	}
        }
        
        String originalName2="";
        String evaluateFileId=slLoan.getCreditInformation();
        if(StringUtils.isNotEmpty(evaluateFileId)){
        	TbUploadFile evaluateFile=tbUploadFileService.queryById(Integer.parseInt(evaluateFileId));
        	if(null!=evaluateFile){
        		originalName2= evaluateFile.getOriginalName();
        	}else{
        		originalName2="文件保存失效，请重新上传";
        	}
        }
        
        Map ret=new HashMap();
        ret.put("realName", realName);
        ret.put("platformUsers", platformUsers);
		ret.put("upDownStreamContractFile", originalName);
		ret.put("evaluateFile", originalName2);
        
        return new Result(Result.Status.OK, ret);

    }
    
    
    @ResponseBody
    @RequestMapping(value = "/queryRealName", method = RequestMethod.GET)
    public Result queryRealName(HttpServletRequest request) {
        SessionUser uservo = CommonUtils.getSessionUser(request);
        int joinid = uservo.getId();
        TbMerchantInfo info = tbMerchantInfoBaseService.queryInfoByJoinId(joinid);
        if(null!=info){
        	String displayBranchNo=branchBaseService.queryDisplayBranchNo(info.getBranchNo());
        	info.setDisplayBranchNo(displayBranchNo);
        }
        return new Result(Result.Status.OK, info);

    }

    @ResponseBody
    @RequestMapping(value = "/applySL", method = RequestMethod.POST)
    public Result applySL(HttpServletRequest request) {
        SessionUser suser = CommonUtils.getSessionUser(request);
        int liveid = slLoanApplicationService.insertTbSlApplication(suser.getId());
        return new Result(Result.Status.OK, liveid);
    }

    @ResponseBody
    @RequestMapping(value = "/clearAndAddMuti", method = RequestMethod.POST)
    public Result clearAndAddMuti(HttpServletRequest request, @RequestBody List<TbSlPlatformUser> entitys) {
        SessionUser suser = CommonUtils.getSessionUser(request);
        Integer joinId = suser.getId();
        if (CollectionUtils.isNotEmpty(entitys)) {
            for (TbSlPlatformUser puser : entitys) {
            	puser.buildDefaultEntity();
                puser.setIdOfTbMerchantUser(joinId);
                puser.encPassword();
            }
            service.insertAndDeleteSlPlatformUser(entitys);
        }

        return new Result(Status.OK, "保存成功");
    }

    @ResponseBody
    @RequestMapping(value = "/stepOneCommit", method = RequestMethod.POST)
    public Result stepOneCommit(HttpServletRequest request, @RequestBody List<TbSlPlatformUser> entitys) {
        SessionUser suser = CommonUtils.getSessionUser(request);
        Integer joinId = suser.getId();
        //前置校验
        TbMerchantInfo info = tbMerchantInfoBaseService.queryInfoByJoinId(joinId);
        if(null!=info){
        	List<Pair> valid35 = info.valid();
			if(CollectionUtils.isNotEmpty(valid35)){
        		return new Result(Result.Status.ERROR, valid35);
        	}
			
			List<Pair> validBean=CommonUtils.beanValid(info);
			if(CollectionUtils.isNotEmpty(validBean)){
        		return new Result(Result.Status.ERROR, validBean);
        	}
        
        }else{
        	return new Result(Result.Status.ERROR, Pair.of("realName", "您还没有填写资质资料"));
        }
        
        TbSlLoanApplication slLoan = slLoanApplicationService.queryTbSlApplicationByJoinId(joinId);
         
        String UpDownStreamContractFile=slLoan.getContract();
        String EvaluateFile=slLoan.getCreditInformation();
        if(StringUtils.isEmpty(UpDownStreamContractFile)){
        	return new Result(Result.Status.ERROR,Pair.of("upDownStreamContractFile", "上下游合同没有上传!"));
        }
        
        if(StringUtils.isEmpty(EvaluateFile)){
        	return new Result(Result.Status.ERROR, Pair.of("evaluateFile", "供应商基本信息数据表没有上传!"));
        }
        
        //授信类型
        if(null != slLoan.getCreditType() && ISLConstDef.SLLOAN_CREDITTYPE_VAGUE == slLoan.getCreditType()){
//        	List<TbSlPlatformUserVO> platforms = service.selectAllPlatformUserListByJoinID(joinId,null);
        	if(CollectionUtils.isEmpty(entitys)){
        		return new Result(Result.Status.ERROR, Pair.of("platforms", "至少绑定一个平台账户信息!"));
        	}
        }
        
        if (CollectionUtils.isNotEmpty(entitys)) {
            for (TbSlPlatformUser puser : entitys) {
            	puser.buildDefaultEntity();
            	puser.setId(null);
                puser.setIdOfTbMerchantUser(joinId);
                puser.encPassword();
            }
            service.insertAndDeleteSlPlatformUser(entitys);
        }
        
        //设置申请平台的上下游合同(同一个合同zip)
        service.updateApplyPlatformWithUpDownStreamContract(joinId,UpDownStreamContractFile);
        
        tbUploadFileService.updateStatusById(Integer.parseInt(UpDownStreamContractFile),IConstDef.UPLOADSTATUS_COMMITED);
        tbUploadFileService.updateStatusById(Integer.parseInt(EvaluateFile),IConstDef.UPLOADSTATUS_COMMITED);
        
        try {
        	//更新申请状态和流程状态
        	slLoanApplicationService.updateLoanStatusAndProcessStatus(slLoan.getCreditType(), 
        		joinId, slLoan.getId(), entitys);
        } catch(BusinessException e) {
        	return new Result(Result.Status.ERROR, e.getMessage());
        }
        
        return new Result(Result.Status.OK, "您的业务申请已提交成功");
    }
    
  	@Autowired
  	private TaskService taskService;
  	
  	
    @ResponseBody
    @RequestMapping(value = "/queryPlatformUser", method = RequestMethod.GET)
    public Result queryPlatformUser(HttpServletRequest request) {
        SessionUser suser = CommonUtils.getSessionUser(request);
        Integer joinId = suser.getId();
        List<TbSlPlatformUserVO> platforms = service.selectAllPlatformUserListByJoinID(joinId,null);
        for(TbSlPlatformUserVO platformUser:platforms){
        	platformUser.decPassword();
        }
        return new Result(Status.OK, platforms);
    }
    
    
    
    @ResponseBody
    @RequestMapping(value = "/bindAlipay", method = RequestMethod.POST)
    public Result bindAlipay(HttpServletRequest request,TbSlAlipayInfo alipay) {
        SessionUser suser = CommonUtils.getSessionUser(request);
        Integer joinId = suser.getId();
        alipay.setIdOfTbMerchantUser(joinId);
        slAlipayInfoService.bindAlipayInfo(alipay);
        tbUploadFileService.updateStatusById(alipay.getAlipayPaperSeal(),IConstDef.UPLOADSTATUS_COMMITED);
        return new Result(Status.OK, "设置支付宝成功");
    }
    
    @ResponseBody
    @RequestMapping(value = "/deletePlatformUser", method = RequestMethod.POST)
    public Result deletePlatformUser(HttpServletRequest request,String platformuserid) {
        SessionUser suser = CommonUtils.getSessionUser(request);
        Integer joinId = suser.getId();
        service.deleteByJoinAndId(joinId, Integer.parseInt(platformuserid));
        return new Result(Status.OK, "删除成功");
    }
    
    @ResponseBody
    @RequestMapping(value = "/pusePlatform", method = RequestMethod.POST)
    public Result pusePlatform(HttpServletRequest request,String platformuserid) {
        SessionUser suser = CommonUtils.getSessionUser(request);
        Integer joinId = suser.getId();
        if(StringUtils.isEmpty(platformuserid)){
        	throw new BusinessException("没有指定平台");
        }
        service.updatePlatformUserEnableflagByPIdAndJId(Integer.parseInt(platformuserid) ,joinId, ISLConstDef.PLATFORM_STATUS_PAUSED);
        return new Result(Status.OK, "申请停用平台成功");
    }
    
    @ResponseBody
    @RequestMapping(value = "/enablePlatform", method = RequestMethod.POST)
    public Result enablePlatform(HttpServletRequest request,String platformuserid) {
        SessionUser suser = CommonUtils.getSessionUser(request);
        Integer joinId = suser.getId();
        if(StringUtils.isEmpty(platformuserid)){
        	throw new BusinessException("没有指定平台");
        }
        service.updatePlatformUserEnableflagByPIdAndJId(Integer.parseInt(platformuserid) ,joinId, ISLConstDef.PLATFORM_STATUS_INITED);
        return new Result(Status.OK, "申请启用平台成功");
    }
    
    
    @ResponseBody
    @RequestMapping(value = "/queryPlatformLoanInfo", method = RequestMethod.GET)
    public Result queryPlatformLoanInfo(HttpServletRequest request) {
        SessionUser suser = CommonUtils.getSessionUser(request);
        Integer joinId = suser.getId();
        List<SlLoanNotPaybackVo> platformLoanInfos = slLoanInfoService.queryPlatformLoanInfoByJoinid(joinId);
        return new Result(Status.OK, platformLoanInfos);
    }
    
    
    @ResponseBody
    @RequestMapping(value = "/changePlatformPwd", method = RequestMethod.POST)
    public Result changePlatformPwd(HttpServletRequest request,String oldPassword,String id,String password) {
        SessionUser uservo = CommonUtils.getSessionUser(request);
        int joinid = uservo.getId();
        //密码允许使用特殊字符
        password=StringEscapeUtils.unescapeHtml4(password);
        oldPassword=StringEscapeUtils.unescapeHtml4(oldPassword);		
        		
        if(StringUtils.isEmpty(id)){
        	throw new BusinessException("平台不允许为空!");
        }
        
        if(StringUtils.isEmpty(oldPassword)){
        	throw new BusinessException("原密码不允许为空!");
        }
        
        if(StringUtils.isEmpty(password)){
        	throw new BusinessException("新密码不允许为空!");
        }
        
        service.updateTbSlPlatFormUserPwd(joinid, Integer.parseInt(id),oldPassword, password);
        return new Result(Result.Status.OK, "修改成功");

    }
    
    @ResponseBody
    @RequestMapping(value = "/applyPausePlatform", method = RequestMethod.POST)
    public Result applyPausePlatform(HttpServletRequest request,String platformuserid) {
        SessionUser uservo = CommonUtils.getSessionUser(request);
        int joinid = uservo.getId();
        if(StringUtils.isEmpty(platformuserid)){
        	throw new BusinessException("平台不允许为空!");
        }
        try{
        service.updatePlatformUserEnableflagByJoinuserId(joinid, ISLConstDef.PLATFORM_STATUS_PAUSED);
            return new Result(Result.Status.OK, "申请成功");
        }catch (Exception e){
            if(e instanceof BusinessException){
                return new Result(Status.ERROR,e.getMessage());
            }
            return new Result(Status.ERROR,"申请平台暂停失败");
        }


    }
    
    
    @ResponseBody
    @RequestMapping(value = "/downloadSLResFile", method = RequestMethod.GET)
    public String downloadEvaluateFile(
            HttpServletRequest request,
            HttpServletResponse response, String fileType) throws IOException {

        if (StringUtils.isEmpty(fileType)) {
            response.setContentType("text/html;charset=utf-8");
            response.setCharacterEncoding(Constants.ENCODING);
            response.getWriter().write("参数不合法！");
            return null;
        }

        String fileName = null;
        String fileNameDisplay = null;
        if (fileType.equals("EVALUATE")) {
            fileName = ISLConstDef.EVALUATE_FILE_NAME;
            fileNameDisplay = ISLConstDef.EVALUATE_FILE_DISPLAYNAME;
        } else if (fileType.equals("SECRETPROMISE")) {
            fileName = ISLConstDef.SECRETPROMISE;
            fileNameDisplay = ISLConstDef.SECRETPROMISE_DISPLAYNAME;
        }

        InputStream resourceAsStream = SlApplyController.class
                .getResourceAsStream("/SLload/" + fileName);
        ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
        IOUtils.copy(resourceAsStream, byteOS);

        DownloadUtils.download(request, response, fileNameDisplay, byteOS.toByteArray());
        LogUtils.info(log, "用户下载 %s", fileName);

        return null;
    }


    @ResponseBody
    @RequestMapping(value = "/genSLContractFile", method = RequestMethod.GET)
    public void genSLContractFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SessionUser uservo = CommonUtils.getSessionUser(request);
        TbSlLoanApplication slLoan = slLoanApplicationService.queryTbSlApplicationByJoinId(uservo.getId());
        if (null == slLoan || StringUtils.isEmpty(slLoan.getMaterialDownload())) {
            throw new ControllerException("不存在可下载的保理合同");
        }

        String materialDownload = slLoan.getMaterialDownload();
        String fileDisplayName=IConstDef.DOWNLOAD_CONTRACTFILE;
        if("preview".equals(request.getParameter("type"))&&StringUtils.isNotEmpty(materialDownload)){
        	materialDownload=materialDownload.replace(".pdf", ".html");
        	fileDisplayName=IConstDef.PREVIEW_CONTRACTFILE;
        }
        
        DownloadUtils.download(request, response, FileUploadUtils.getDefaultBaseDir() + File.separator + materialDownload,fileDisplayName);
        LogUtils.info(log, "下载%s", fileDisplayName);
    }

    @ResponseBody
    @RequestMapping(value = "/agreeSLLoanFee", method = RequestMethod.POST)
    public Result agreeSLLoanFee(HttpServletRequest request) {
        SessionUser suser = CommonUtils.getSessionUser(request);
        Integer joinId = suser.getId();
        slLoanApplicationService.updateAgreeSLLoanFee(joinId);
        TbSlLoanApplication slLoan = slLoanApplicationService.queryTbSlApplicationByJoinId(joinId);
        return new Result(Result.Status.OK, slLoan);
    }

    /**
	 * 
	 * 用户提交保理合同
	 * @param request
	 * @return
	 */
    @RequestMapping(value = "/uploadSLContractFile", method = RequestMethod.POST)
    public void uploadSLContractFile(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "uploadFile", required = false) MultipartFile file) throws IOException {

        Result res = CommonUtils.getPathUploadedFile(request, response, file, ISLConstDef.slcontractUserFileDir);
        if (Status.ERROR == res.getStatus()) {
            CommonUtils.sendJOSNResponse(response, res);
            return;
        }

        String filePath = String.valueOf(res.getMessage());
        String filename = file.getOriginalFilename();
        int joinuserid = CommonUtils.getSessionUser(request).getId();
        TbUploadFile uploadFile=new TbUploadFile();
        uploadFile.setDownloadPath(filePath);
        uploadFile.setOriginalName(filename);
        uploadFile.setIdOfTbMerchantUser(joinuserid);
        uploadFile.setUploadOperator(String.valueOf(joinuserid));
        uploadFile.setFileType(ISLConstDef.SUPPLY_FACTORING_CONTRACT_USERCOMMIT);
        tbUploadFileService.insert(uploadFile);
        
        TbSlLoanApplication slLoan = slLoanApplicationService.queryTbSlApplicationByJoinId(joinuserid);
        if (null == slLoan) {
            CommonUtils.sendJOSNResponse(response, new Result(Status.ERROR, "您没有申请供应链贷."));
        } else {
            slLoanApplicationService.updateMaterial(joinuserid, String.valueOf(uploadFile.getId()),slLoan.getId());
            CommonUtils.sendJOSNResponse(response, new Result(Status.OK, filename + "文件上传成功！"));
        }
    }
    
    /**
     * 下载展期还款申请书
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/downloadSLRenewedApply", method = RequestMethod.GET)
    public void downloadSLRenewedApply(
            HttpServletRequest request, HttpServletResponse response, int creditlogid) throws IOException{
    	SessionUser suser = CommonUtils.getSessionUser(request);
        Integer joinuserid = suser.getId();
        
        if(CommonUtils.isEmpty(creditlogid)){
            throw new BusinessException("待还贷款编号不能为空.");
        }
        
    	String filePath = slLoanApplicationService.generateSLRenewedApply(joinuserid, creditlogid);
    	
    	String download_filepath = ISLConstDef.slLoanRenewedApplyDir + File.separator
				+ FileUploadUtils.getFileNameFromPath(filePath);
    	
    	String filename = IConstDef.DOWNLOAD_RENEWEDAPPLY + ".pdf";
		
		DownloadUtils.download(request, response, FileUploadUtils.getDefaultBaseDir() + File.separator + download_filepath, filename);
		LogUtils.info(log, "下载%s",IConstDef.DOWNLOAD_RENEWEDAPPLY);
    }
}
